#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Create your views here.
import json
from django.http import JsonResponse
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
import pymongo

from application.settings import mongo_cli

from vehicle.models import VehicleInfoModel, VehicleFenceModel, VehicleMoveInfoModel, VehicleFaultInfoModel, \
    VehicleFaultLogModel
from vehicle.serializers import (
    VehicleInfoModelSerializer,
    VehicleFenceModelSerializer,
    VehicleMoveInfoModelSerializer,
    VehicleFaultInfoModelSerializer,
    VehicleFaultLogModelSerializer

)

from dvadmin.utils.viewset import CustomModelViewSet
from application.settings import redis_cli


class VehicleInfoModelViewSet(CustomModelViewSet):
    """
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = VehicleInfoModel.objects.all()
    serializer_class = VehicleInfoModelSerializer
    filter_fields = ['vehicle_type', 'vehicle_status', 'vehicle_motor', 'vehicle_battery']
    search_fields = ['vin']


class VehicleFenceModelViewSet(CustomModelViewSet):
    """
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = VehicleFenceModel.objects.all()
    serializer_class = VehicleFenceModelSerializer
    # 精确
    filter_fields = ['status', 'fence_alarm_type']
    # 模糊
    search_fields = ['fence_name', 'fence_alarm_type', 'status']


class VehicleMoveInfoModelViewSet(CustomModelViewSet):
    """
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = VehicleMoveInfoModel.objects.all()
    serializer_class = VehicleMoveInfoModelSerializer
    # 精确
    filter_fields = ['vehicle_vin']
    # 模糊
    search_fields = ['vehicle_vin']


class VehicleFaultInfoModelViewSet(CustomModelViewSet):
    """
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = VehicleFaultInfoModel.objects.all()
    serializer_class = VehicleFaultInfoModelSerializer
    # 精确
    filter_fields = ['fault_type', 'fault_group']
    # 模糊
    search_fields = ['fault_label', 'fault_code', 'fault_site']


class VehicleFaultLogModelViewSet(CustomModelViewSet):
    """
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = VehicleFaultLogModel.objects.all()
    serializer_class = VehicleFaultLogModelSerializer
    # 精确
    filter_fields = ['vin', 'fault_code', 'log_status']
    # 模糊
    search_fields = ['vin', 'fault_code', 'log_status']


import json
import time

import redis
import re
from django.http import JsonResponse, HttpResponse

# r.set())
# print(r.get('we')))
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, decode_responses=True)
r = redis.Redis(connection_pool=pool)
VEHICLE_DATA_DICT = {
    "vin": "vin",
    "motorCurrent": "电机电流",
    "motorSpeed": "电机转速",
    "motorTemperature": "电机温度",
    "motorTorque": "电机转矩",
    "motorVoltage": "电机电压",
    "gear": "档位",
    "remainingBattery": "剩余电量",
    "brakePedal": "制动踏板行程值",
    "fuelConsumptionRate": "燃料消耗率",
    "resistance": "绝缘电阻",
    "singleBatteryMaxTemperature": "单体电池最高温度",
    "singleBatteryMaxVoltage": "单次最大电压",
    "singleBatteryMinTemperature": "单体电池最低温度",
    "singleBatteryMinVoltage": "单体电池最低电压",
    "totalBatteryCurrent": "动力电池充放电电流",
    "totalBatteryVoltage": "动力电池负载端总电压V3",
}

from django.views import View


class vehicle_s(View):
    def get(self, request, pk, ):
        # print(pk)
        common_keys = (
            "latitude", "longitude", "speed", "voltage",
            "singleBatteryMinTemperature")
        data = r.lpop(pk)
        print(type(data))
        real_info = dict()
        if data:
            real_info = json.loads(data)
            print(type(real_info))
            common_info = {k: v for k, v in real_info.items() if k in common_keys}
            real_info = {v: real_info.get(k) for k, v in VEHICLE_DATA_DICT.items()}
            real_info.update(common_info)
        return JsonResponse(dict(code=2000, data=real_info))
